package com.chlitina.store.modules.sms.utils;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

//中国网建
public class SMSSend1 {
	private static String Uid = "i_am_bob";
	private static String Key = "5f8d373331b72502b399";
	private static String Url = "http://gbk.sms.webchinese.cn";
	final public static int ONE_MES_length = 64;//一条短信的默认长度
	
	public static String send(String phone, String message) throws Exception{
		 HttpClient client = new HttpClient();
		 PostMethod post = new PostMethod(Url);
		 post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk");//在头文件中设置转码
		 NameValuePair[] data ={ 
				 new NameValuePair("Uid", Uid),
				 new NameValuePair("Key", Key),
				 new NameValuePair("smsMob", phone),
				 new NameValuePair("smsText", message)
		 };
		 post.setRequestBody(data);
		 client.executeMethod(post);
		 int statusCode = post.getStatusCode();
		 if(statusCode != 200){  
			 throw new Exception("网络发送异常");
		 }  
		 String result = new String(post.getResponseBodyAsString().getBytes("gbk"));
		 post.releaseConnection();
		 return result;
	}
	
	public static String getSmsReaminNum() {
		HttpClient client = new HttpClient();
		String restMsg = "";
		GetMethod get = null;
		try {
			get = new GetMethod(
					"http://sms.webchinese.cn/web_api/SMS/?Action=SMS_Num&Uid="+ Uid + "&Key=" + Key);
			int status = client.executeMethod(get);
			if (status == 200) {
				restMsg = get.getResponseBodyAsString();
			}
			get.releaseConnection();
		} catch (Exception e) {
			if (get != null) {
				get.releaseConnection();
			}
			System.out.println("查询异常");
		}
		return restMsg;
	}
	
	@SuppressWarnings("unused")
	private static String getErrorMessage(int result) {
		switch (result) {
		case -1:
			return "没有该用户账户";
		case -2:
			return "密钥不正确 ";
		case -3:
			return "短信数量不足";
		case -11:
			return "该用户被禁用";
		case -14:
			return "短信内容出现非法字符";
		case -4:
			return "手机号格式不正确";
		case -41:
			return "手机号码为空";
		case -42:
			return "短信内容为空";
		default:
			return "短信发送异常";
		} 
	}
	
	public static void main(String[] args) {
		try {
			HttpClient client = new HttpClient();
			PostMethod post = new PostMethod("http://gbk.sms.webchinese.cn");
			post.addRequestHeader("Content-Type",
					"application/x-www-form-urlencoded;charset=gbk");// 在头文件中设置转码
			NameValuePair[] data = { new NameValuePair("Uid", "i_am_bob"),
					new NameValuePair("Key", "5f8d373331b72502b399"),
					new NameValuePair("smsMob", "15902991399"),
					new NameValuePair("smsText", "1234567823456789012341") };
			post.setRequestBody(data);

			client.executeMethod(post);
			Header[] headers = post.getResponseHeaders();
			int statusCode = post.getStatusCode();
			if(statusCode != 200){  
			    throw new Exception();
			}  
			System.out.println("statusCode:" + statusCode);
			for (Header h : headers) {
				System.out.println(h.toString());
			}
			String result = new String(post.getResponseBodyAsString().getBytes("gbk"));
			System.out.println("result-------------  "+Integer.parseInt(result));

			post.releaseConnection();
		} catch (Exception e) {

		}
	}
}
